#include "myhead.h"
#include "head.h"


//用户登录
void User_menu()
{
    int choice;
    do
    {
        CLEAR_SCREEN();
        MOVE_CURSOR(5,25);
        printf("请输入选项：");

        
        MOVE_CURSOR(8,26);
        SET_COLOR(COLOR_YELLOW,0);
        printf("1.查找书籍");

        MOVE_CURSOR(11,26);
        printf("2.借书");

        MOVE_CURSOR(14,26);
        printf("3.还书");

        MOVE_CURSOR(17,26);
        printf("4.借书查询");

        MOVE_CURSOR(20,26);
        printf("5.退出");

    choice = getchoice(1,5,22,26);

    switch (choice)
    {
    case 1:
        Find();
        break;

    case 2:
        Borrow();
        break;
    
    case 3:
        Return();
        break;

    case 4:
        borrowingInquiry();
        break;

    case 5:
        
        return ;
    }
        
    } while (1);
}

//查找书籍
void Find()
{
    int num;
    int choice;
    int found = 0;
    char find0[100];
    int count = 0;

    CLEAR_SCREEN();
    MOVE_CURSOR(5,26);
    SET_COLOR(COLOR_YELLOW,0);
    printf("查找书籍");

    MOVE_CURSOR(8,26);
    printf("1.按书号查找：");

    MOVE_CURSOR(11,26);
    printf("2.按书名查找：");

    MOVE_CURSOR(14,26);
    printf("3.按作者查找：");

    choice = getchoice(1,3,17,26);
    switch (choice)
    {
    case 1:
        CLEAR_SCREEN();
        SET_COLOR(COLOR_YELLOW,0);
        MOVE_CURSOR(5,26);
        printf("输入书号：");
        scanf("%d",&num);
        getchar();

        CLEAR_SCREEN();
        MOVE_CURSOR(5,26);
        printf("查找结果：");
        for (int i = 0; i < book_count; i++)
        {
            if (num == books[i].Number)
            {
                MOVE_CURSOR(6 + count, 26);
                printf("书号：%d 书名：%s 作者：%s",books[i].Number,books[i].Title,books[i].Author); 
                found = 1; 
                count += 1;    
            }
        }
        MOVE_CURSOR(8 + count,26);
        SET_COLOR(COLOR_TITLE,0);
        Enter();
        RESET_COLOR();
        break;

    case 2:
        CLEAR_SCREEN();
        SET_COLOR(COLOR_YELLOW,0);
        MOVE_CURSOR(5,26);
        printf("输入书名：");
        getinput(find0,sizeof(find0),5,35);
     

        CLEAR_SCREEN();
        MOVE_CURSOR(5,26);
        printf("查找结果：");
        for (int i = 0; i < book_count; i++)
        {
            if (strcmp(find0 , books[i].Title) == 0) //字符串比较
            {
                MOVE_CURSOR(6 + count, 26);
                printf("书号：%d 书名：%s 作者：%s",books[i].Number,books[i].Title,books[i].Author);  
                found = 1;
                count += 1;   
            }
        }
        MOVE_CURSOR(8 + count,26);
        SET_COLOR(COLOR_TITLE,0);
        Enter();
        RESET_COLOR();
        break;

    case 3:
        CLEAR_SCREEN();
        SET_COLOR(COLOR_YELLOW,0);
        MOVE_CURSOR(5,26);
        printf("输入作者：");
        getinput(find0,sizeof(find0),5,35);
       

        CLEAR_SCREEN();
        MOVE_CURSOR(5,26);
        printf("查找结果：");
        for (int i = 0; i < book_count; i++)
        {
            if (strcmp(find0 ,books[i].Author) == 0)
            {
                MOVE_CURSOR(6 + count , 26);
                printf("书号：%d 书名：%s 作者：%s",books[i].Number,books[i].Title,books[i].Author); 
                found = 1;  
                count += 1;     
            }
        }
        MOVE_CURSOR(8 + count,26);
        SET_COLOR(COLOR_TITLE,0);
        Enter();
        RESET_COLOR();
        break;
    }
    if(!found)
    {
        SET_COLOR(COLOR_ERROR,0);
        printf("\n找不到");
        MOVE_CURSOR(19,26);
        SET_COLOR(COLOR_TITLE,0);
        Enter();
    }
getchar();
}

//借书
void Borrow()
{   
    int num;
    int index = -1;
    int found = 0;
    CLEAR_SCREEN();
    
    MOVE_CURSOR(5,26);
    printf("借书：");
    SET_COLOR(COLOR_YELLOW,0);

    MOVE_CURSOR(8,26);
    printf("借阅书籍书号：");
    scanf("%d",&num);
    getchar();
    
    for (int i = 0; i < book_count; i++)
    {
        if (num == books[i].Number)
        {
            found = 1;//
            if (books[i].Status)
            {
                SET_COLOR(COLOR_ERROR,0);
                printf("该书以被借出");
            }
            else{
                books[i].Status = 1;
                if (Borrower_count<20)
                {
                    Borrower[Borrower_count] = books[i];
                    Borrower_count++;
                    printf("借阅成功");
                }
                else
                {
                    SET_COLOR(COLOR_ERROR,0);
                    printf("借阅记录已满");
                }
            }
            break;          
        } 
    }
    if(!found){
        SET_COLOR(COLOR_ERROR,0);
        printf("找不到该书");
}
MOVE_CURSOR(10,26);
SET_COLOR(COLOR_TITLE,0);
Enter();
getchar();
}

//还书
void Return()
{
    int num;
    int found = 0;
    int borrowed = 0;

    CLEAR_SCREEN();
    MOVE_CURSOR(5,26);
    printf("还书");

    MOVE_CURSOR(8,26);
    SET_COLOR(COLOR_YELLOW,0);
    printf("输入还书书号：");
    scanf("%d",&num);
    getchar();

    for (int i = 0; i < book_count; i++)
    {
        if (num == books[i].Number)
        {
            found = 1;
            if(books[i].Status)
            {
                books[i].Status = 0;
            
                for (int j = 0; j < Borrower_count; j++)
                {
                    if (num == Borrower[j].Number)
                    {
                        for (int k = j; k < Borrower_count-1; k++)
                        {
                            Borrower[k] = Borrower[k+1];
                        }
                        Borrower_count--;
                        borrowed = 1;
                        break; 
                    }
                }
            }
        break ;
        }
    }
    if (found)
    {
        if (borrowed)
        {
            MOVE_CURSOR(9,26);
            SET_COLOR(COLOR_SUCCESS,0);
            printf("还书成功：");          
        }
        else
        {
            MOVE_CURSOR(9,26);
            SET_COLOR(COLOR_ERROR,0);
            printf("此书未被借出：");
        }
        
    }
    else
    {
        MOVE_CURSOR(9,26);
        SET_COLOR(COLOR_ERROR,0);
        printf("未找到此书：");
    }
    MOVE_CURSOR(10,26);
    SET_COLOR(COLOR_TITLE,0);
    Enter();    
    RESET_COLOR();
    getchar();
}

//借阅查询
void borrowingInquiry()
{
    CLEAR_SCREEN();
    MOVE_CURSOR(5,26);
    printf("借阅查询");

    SET_COLOR(COLOR_YELLOW,0);
    if(Borrower_count >0)
    {
        MOVE_CURSOR(7,26);
        printf("%-10s %-20s %-15s %-10s", "书号", "书名", "作者", "状态");
        for (int i = 0; i < Borrower_count; i++)
        {
            MOVE_CURSOR(9 + i, 26);
            printf("%-10d %-20s %-15s %-10s", 
                Borrower[i].Number,Borrower[i].Title,Borrower[i].Author,"已借阅"); 
                MOVE_CURSOR(11+i,26);
        }
    }
    else
    {
        MOVE_CURSOR(8,26);
        SET_COLOR(COLOR_ERROR,0);
        printf("没有借阅记录");
        MOVE_CURSOR(10,26);
    }
    SET_COLOR(COLOR_TITLE,0);
    Enter();
getchar();
}

